<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form ref="form" label-width="100px" :model="userInfo" :rules="rules">
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input
              v-model="userInfo.departmentName"
              placeholder="请选择部门"
              @focus="showDept = true"
            />
            <!-- 需求: 选择部门 显示到上边的输入框 -->
            <el-row v-show="showDept" class="selBox">
              <!-- 关闭按钮 -->
              <i class="el-icon-close"></i>
              <el-tree
                ref="tree"
                :data="departData"
                :props="{ label: 'name' }"
                @node-click="selDept"
              >
              </el-tree>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <el-image :src="require('@/assets/common/head.jpg')"></el-image> -->
            <UploadImg :staff-photo.sync="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="updateUserInfo">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils'
import { mapActions } from 'vuex'
export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      showDept: false,
      // 部门数据
      departData: [],
      // 表单数据 结合后台新增员工文档
      departmentName: '', // 部门
      // rules: {
      //   departmentName: [
      //     { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
      //   ]
      // }
      rules: {
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getDepartData()
  },
  methods: {
    ...mapActions('user', ['getUserInfoAction']),
    // 选择部门
    selDept (currDept) {
      console.log(currDept)
      this.userInfo.departmentName = currDept.name
      this.showDept = false
    },
    // 获取部门数据
    async getDepartData () {
      const { depts } = await getDepartments()
      // console.log(depts)
      // 转换树形数据
      // 注意: 调用一次
      this.departData = transformTreeData(depts)
    },

    async updateUserInfo () {
      try {
        await this.$refs.form.validate()
        await saveUserDetailById(this.userInfo)
        // 更新右上角显示的登录人信息=> 重新调用 vuex的Action方法
        this.getUserInfoAction()
        this.$message.success('修改成功')
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
